#include "stdafx.h"
#include "afxwin.h"
#include "afxcoll.h"
#include "..\include\Carriage.h"
#include "..\MSVC\train\resource.h"


Carriage::Carriage(int turn) {
    BITMAP bm;
    bmpCarriage.LoadBitmap(IDB_CARRIAGE);
    bmpCarriage.GetBitmap(&bm);
    int bmpWidth = bm.bmWidth;
    int bmpHeight = bm.bmHeight;

    xPosition = turn * bmpWidth;
    yPosition = 0;

    isActive = false;
    isStatic = false;
    
}

Carriage::~Carriage() {
}

void Carriage::SetTrack(const Track& point) {
    int bkWidth = 900;
    int bkHeight = 600;

    ID = point.CarID;
    source = point.source;
    dest = point.dest;
    if (point.source == -1) {
    } else {
    }
    if (point.dest == 0) {
        xFinalPos = 900 - point.CarID * 30;
        yFinalPos = 0;
    } else {
        xFinalPos = 300 + point.dest * 2 * 30;
        yFinalPos = 300 - point.posInStack * 30;
               // AfxMessageBox(TEXT("fuck"));
    }
}

void Carriage::SetID(int carID) {
    ID = carID;
}

int Carriage::GetID(void) {
    return ID;
}

Carriage::Carriage(const Track& point) {
    xPosition = 0;
    yPosition = 0;
    CRect rect;
    //GetClientRect(&rect);
    //int bkWidth = rect.Width();
    //int bkHeight = rect.Height();
    int bkWidth = 800;
    int bkHeight = 500;

    BITMAP bm;
    bmpCarriage.LoadBitmap(IDB_CARRIAGE);
    bmpCarriage.GetBitmap(&bm);
    int bmpWidth = bm.bmWidth;
    int bmpHeight = bm.bmHeight;

    ID = point.CarID;
    if (point.source == -1) {
        xFinalPos = 0;
        yFinalPos = 0;
    } else {
    }
    if (point.dest == -1) {
        yFinalPos = 0;
        xFinalPos = 300 + point.CarID * bmpWidth;
    } else {
        xFinalPos = 100 + point.dest * 2 * bmpHeight;
        yFinalPos = bkHeight - point.posInStack * bmpWidth;
    }
}


void Carriage::draw(CDC *MemDC) {
    BITMAP bmInfo;
    CDC bmpDC;
    bmpDC.CreateCompatibleDC(NULL);

   
    bmpCarriage.GetBitmap(&bmInfo);

    bmpDC.SelectObject(bmpCarriage);
    CString str;
    str.Format(TEXT("%d"), ID);
    
    bmpDC.TextOut(10, 10,str, str.GetLength());

    MemDC->BitBlt(xPosition, yPosition, bmInfo.bmWidth, bmInfo.bmHeight, &bmpDC, 0, 0, SRCCOPY);
    bmpDC.DeleteDC();
}
    
void Carriage::move() {
    if (!isActive) {
        return;
    }
    if (source == -1 && dest == 0) {
        //AfxMessageBox(TEXT("fuck"));
        if (xPosition < xFinalPos) {
            xPosition += 3;
        } else {
            isStatic = true;
        }
        return;
    }
    if (source == -1 && dest !=0) {
        if (xPosition < xFinalPos) {
            xPosition += 3;
        } else if (yPosition < yFinalPos) {
            yPosition += 3;
        } else {
            isStatic = true;
        }
        return;
    }

    if (dest== 0) {
         //AfxMessageBox(TEXT("fuck"));
        if (yPosition > yFinalPos) {
            yPosition -= 3;
        } else if (xPosition < xFinalPos) {
            xPosition += 3;
        } else {
            isStatic = true;
        }
    }
}

void Carriage::enable(bool en = true) {
    isActive = en;
    isStatic = !en;
}

bool Carriage::IsActive() {
    return isActive;
}

bool Carriage::IsStatic(void) {
    return isStatic;
}